package com.vodone.v1pin.component.core
{
	import caurina.transitions.Tweener;
	
	import com.vodone.v1pin.core.event.V1webEventDispatcher;
	import com.vodone.v1pin.userInfo.event.CloseWindowEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Image;
	import mx.controls.SWFLoader;
	import mx.events.FlexEvent;
	import mx.events.PropertyChangeEvent;
	
	public class FeedLoadingImage extends Image
	{
		
		public var initW:int = 200;
		public var initH:int = 200;
		
		public var time:Number = 0.3;
		public function FeedLoadingImage()
		{
			super();
			addEventListener(Event.COMPLETE , completeHandler );
			addEventListener(FlexEvent.CREATION_COMPLETE , creationComplete );
			addEventListener(MouseEvent.MOUSE_WHEEL , mouseWheelHandler );
//			V1webEventDispatcher.getInstance().addEventListener(CloseWindowEvent.CLEAR_EVENT,clear);
		}
		
		private function mouseWheelHandler(e:MouseEvent):void
		{
			e.stopImmediatePropagation();
		}
		
		private function creationComplete(event:FlexEvent):void
		{
			oldContent = new Image();
			addChild( oldContent );
			oldContent.width = this.width;
			oldContent.height = this.height;
		}
		
		private var oldSouce:Object;
		private var bitmapdata:BitmapData;
		public override function set source(value:Object):void
		{
//			return;
			if( oldSouce == value ) return;
			if( oldContent ){
//				oldContent.width = this.width;
//				oldContent.height = this.height;
				if(bitmapdata){
					bitmapdata.dispose();
				}
				if( this.source ){
					if( this.width>0 && this.height>0 ){
						bitmapdata = new BitmapData(this.width,this.height);
						bitmapdata.draw(this);
						oldContent.source = new Bitmap(bitmapdata);
						oldContent.alpha = 1;
						Tweener.addTween( oldContent,{alpha:0,time:time,transition:"linear"} );
					}
				}
			}
//			if( this.content ){
//				this.content.alpha = 0;
//			}
			oldSouce = value;
			super.source = value;
			if( null != loading )
			{
//				hideLoading();
				showLoading();
			}
		}
		
		
		public function showLoading():void
		{
			loading.visible = true;
		}
		public function hideLoading():void
		{
			loading.visible = false;
		}
		private function completeHandler( e:Event ) :void
		{
			hideLoading();
			if( this.content ){
				this.content.alpha = 0;
				Tweener.addTween( this.content,{alpha:1,time:time,transition:"linear"} );
			}else{
				this.content.alpha = 1;
			}
		}
		
		private function endEffect():void
		{
		}
		
		private var loading:SWFLoader = new SWFLoader();
		private var oldContent:Image;
		protected override function createChildren():void
		{
			super.createChildren();
			loading.source = "assets/loading/loading2.swf";
			loading.width = 40;
			loading.height = 40;
			addChild(loading);
		}
		protected override function updateDisplayList(w:Number, h:Number):void
		{
			super.updateDisplayList( w , h );
			var loadingX:Number = ( w - loading.width ) / 2;
			var loadingY:Number = ( h - loading.height ) / 2;
			loading.move( loadingX , loadingY  );
		}
		
		public function clear( event:Event = null ):void
		{
			this.source = null;
			this.oldSouce = null;
			if(oldContent){
				oldContent.source = null;
			}
//			trace("清除");
		}
	}
}